package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyLike;
import cn.wolfcode.trip.base.mapper.StrategyLikeMapper;
import cn.wolfcode.trip.base.service.IStrategyLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StrategyLikeServiceImpl implements IStrategyLikeService {
    @Autowired
    private StrategyLikeMapper strategyLikeMapper;

    @Override
    public Long checkNumByUserIdAndStrategyId(Long userId, Long strategyId) {
        // 根据userId和strategyId查询数据库是否有记录
        StrategyLike strategyLike =  strategyLikeMapper.queryExistLikeByUserIdAndStrategyId(userId,strategyId);
        if (strategyLike==null){
            // 1保存一条记录
            StrategyLike like = new StrategyLike();
            like.setUserId(userId);
            like.setStrategyId(strategyId);
            like.setTime(new Date());
            strategyLikeMapper.insert(like);
            // 2点赞数记录表保存一条数据
            strategyLikeMapper.insertStrategyNum(userId,strategyId,1L);
            // 3查询当前攻略的点赞数
            return strategyLikeMapper.queryNumByStrategyId(strategyId);
        }else {
            // 删除一条记录
            strategyLikeMapper.delete(userId);
            // 2点赞数记录表删除一条数据
            strategyLikeMapper.deleteStrategyNum(userId,strategyId);
            // 3查询当前攻略的点赞数
            return strategyLikeMapper.queryNumByStrategyId(strategyId);
        }
    }

    @Override
    public Long getNumByStrategyId(Long strategyId) {
        return strategyLikeMapper.queryNumByStrategyId(strategyId);
    }
}
